﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WXYW.Entities;
using WXYW.DAL;

namespace WXYW.BLL
{
    public class ProblemReportService : RepositoryBase
    {
        public IQueryable<ReportType> GetReportTypeList()
        {
            return GetEntities<ReportType>().OrderBy(r=>r.Name);
        }

        public IQueryable<ProductLineType> GetProductLineTypeList()
        {
            return GetEntities<ProductLineType>().OrderBy(p=>p.Name);
        }

        public void AddProblemReport(ProblemReport problemReportEntity)
        {
            InsertEntity(problemReportEntity);
            ProblemReport entity = GetEntities<ProblemReport>().Where(p => p.ID == problemReportEntity.ID).FirstOrDefault();
            entity.Code = entity.Code + entity.ID.ToString();
            UpdateEntity(entity);
        }

        public List<ProblemReport> GetProblemReportList(User currentUser, string reportType, string productLineType, string keyword)
        {
            List<ProblemReport> problemReports = null;
            if (currentUser.UserType.ID == (int)RoleType.Administrator)
            {
                problemReports = GetEntities<ProblemReport>().OrderByDescending(p => p.CreateDate).ToList();
            }
            else
            {
                problemReports = GetEntities<ProblemReport>(p => p.User.UserID == currentUser.UserID).OrderByDescending(p => p.CreateDate).ToList();                
            }
            if (!reportType.Equals("0"))
            {
                problemReports = problemReports.Where(p => p.ReportType.Equals(reportType)).ToList();
            }
            if (!productLineType.Equals("0"))
            {
                problemReports = problemReports.Where(p => p.ProductLineType.Equals(productLineType)).ToList();
            }
            if (!string.IsNullOrEmpty(keyword))
            {
                problemReports = problemReports.Where(p => (p.ProblemType != null && p.ProblemType.Contains(keyword)) || (p.ProblemDescription != null && p.ProblemDescription.Contains(keyword)) ||
                    (p.ProblemReason != null && p.ProblemReason.Contains(keyword)) || (p.ProblemStatus != null && p.ProblemStatus.Contains(keyword)) || p.ProductLineType.Contains(keyword) ||
                    p.ProductVersion.Contains(keyword) || p.ReportType.Contains(keyword) || p.Solution.Contains(keyword) ||
                    p.Subject.Contains(keyword) || p.ProductType.Contains(keyword)).ToList();
            }
            return problemReports;
        }

        public List<ProblemReport> GetProblemReportListAll(User currentUser, string reportType, string productLineType, string keyword)
        {
            List<ProblemReport> problemReports = GetEntities<ProblemReport>().OrderByDescending(p => p.CreateDate).ToList(); 
            if (currentUser.UserType.ID == (int)RoleType.ASC)
            {
                problemReports = problemReports.Where(p => p.SecurityType.Equals("ASC")).ToList();
            }            
           
            if (!reportType.Equals("0"))
            {
                problemReports = problemReports.Where(p => p.ReportType.Equals(reportType)).ToList();
            }
            if (!productLineType.Equals("0"))
            {
                problemReports = problemReports.Where(p => p.ProductLineType.Equals(productLineType)).ToList();
            }
            if (!string.IsNullOrEmpty(keyword))
            {
                problemReports = problemReports.Where(p => (p.ProblemType != null && p.ProblemType.Contains(keyword)) || (p.ProblemDescription != null && p.ProblemDescription.Contains(keyword)) ||
                    (p.ProblemReason != null && p.ProblemReason.Contains(keyword)) || (p.ProblemStatus != null && p.ProblemStatus.Contains(keyword)) || p.ProductLineType.Contains(keyword) ||
                    p.ProductVersion.Contains(keyword) || p.ReportType.Contains(keyword) || p.Solution.Contains(keyword) ||
                    p.Subject.Contains(keyword) || p.ProductType.Contains(keyword)).ToList();
            }
            return problemReports;
        }

        public ProblemReport GetProblemReport(string code)
        {
            return GetEntities<ProblemReport>().Where(p => p.Code.Equals(code)).FirstOrDefault();
        }

        public void DeleteProblemReportByCode(long reportID)
        {
            DeleteEntities<ProblemReportFile>(p=>p.ProblemReport.ID == reportID, Guid.Empty);
            DeleteEntities<ProblemReport>(p=>p.ID == reportID, Guid.Empty);
        }

        public void UpdateProblemReport(ProblemReport problemReportEntity)
        {
            UpdateEntity(problemReportEntity);
        }

        public int AddProblemReportFile(string code, string fileName)
        {
            ProblemReportFile problemReportFile = new ProblemReportFile();
            problemReportFile.FileName = fileName;
            problemReportFile.CreateDate = DateTime.Now;
            problemReportFile.ProblemReport = GetEntities<ProblemReport>().Where(p => p.Code.Equals(code)).FirstOrDefault();
            InsertEntity(problemReportFile);
            return problemReportFile.ID;
        }

        public IQueryable<ProblemReportFile> GetReportFiles(string code)
        {
            return GetEntities<ProblemReportFile>().Where(p => p.ProblemReport.Code.Equals(code));
        }

        public void DeleteReportFile(int fileID)
        {
            DeleteEntities<ProblemReportFile>(p=>p.ID == fileID, Guid.Empty);
        }
    }
}
